<?php
/**
 * api接口调试
 */
class ApiController extends Controller
{
    public $navbarName='Api';

    private $dataBase_name = 'wink';

    /**
     * 接口調用方式
     */
    public $type_data=array(array('id'=>1,'name'=>'GET'),array('id'=>2,'name'=>'POST'),array('id'=>3,'name'=>'JSON'));
    /**
     * 調用方式名称数组
     */
    public $type_name=array('1'=>'GET','2'=>'POST','3'=>'JSON');

    //调试主页
    public function actionIndex()
    {
        $this->pageTitle = 'API接口调试';
        $params_data=$_POST['_form'];
        $hosts_cleck=$_POST['hosts_cleck'];

        $params_data['hosts']=$_POST['hosts'];
        $_POST['a']=='新增并调用'?$save_type=true:$save_type=false;

        //请求接口
        if($_POST['_form']){
            $curl_statrs=0;//接口服务器返回状态码

            //当锁定域名时
            if($hosts_cleck==1){$params_data['hosts']=$_POST['hosts_sel'];$params_data['hosts_cleck']=$hosts_cleck;}
            if($save_type && $hosts_cleck!=1){
                $hostsId=ApiHostsInfo::model()->retHostsIdForHostsName($params_data['hosts']);
                $table_data = new ApiUrlInfo();
                $table_data->hosts_id=$hostsId;
                $table_data->attributes = $_POST['_form'];
                $table_data->save();
                $params_data['id']=Yii::app()->db->getLastInsertID();
            }
            //存储接口参数
            ApiParamsInfo::model()->SaveParams($params_data['id'],$params_data['params']);


            //接口调用
            $url=$params_data['hosts'].$params_data['url'];
            if(!empty($url)){
                if( !empty($params_data['params']) && $params_data['type']!=3 ){
                    $params=explode(';',ClearFormat($params_data['params']));
                    $curl_params=array();
                    foreach($params as $k =>$v){
                        if(empty($v)){ continue;}
                        $a=explode(',',$v);
                        if(empty($a[0])){ continue;}
                        $curl_params[$a[0]]=$a[1];
                    }
                }


                if($params_data['type']==1){
                    $params_data['retData']= Yii::app()->curl->get($params_data['hosts'].$params_data['url'], $curl_params);
                }elseif($params_data['type']==2){
                    $params_data['retData']= Yii::app()->curl->post($params_data['hosts'].$params_data['url'], $curl_params);
                }elseif($params_data['type']==3){
                    $params_data['retData']= Yii::app()->curl->json($params_data['hosts'].$params_data['url'], $curl_params);
                }
                $curl_statrs=Yii::app()->curl->_status;
            }


            //记住接口返回参数
            try{
                $api_log=new ApiReturnDataLog();
                $api_log->stats=$curl_statrs;
                $api_log->url=$params_data['hosts'].$params_data['url'];
                $api_log->data=$params_data['retData'];
                if($params_data['id']>0){
                    $api_log->api_id=$params_data['id'];
                }
                $api_log->save();
            } catch (Exception $e) {
                //出错就不要了,之后考虑做个日志存储方法
            }


        }else if($_GET['id']>0){
            //填充接口数据
            $params_data=ApiUrlInfo::model()->IFindId(intval($_GET['id']));
        }
        //查询所有接口信息
        $model=ApiUrlInfo::model()->IFindAll();
        //查询所有域名信息
        $hostsData=ApiHostsInfo::model()->IFindAll();

        $type_data=$this->type_data;
        $this->render('/api/index' ,array('api_data' => $model,'hostsData'=>$hostsData,'type_data'=>$type_data,'params_data'=>$params_data,'curl_statrs'=>$curl_statrs));

    }
    //ajax接口调用
    public function actionAjaxRequestApi(){

    }
    //ajax 填充接口所有数据
    public function actionAjaxFillApi(){
        $this->layout=false;
        $params_data=ApiUrlInfo::model()->IFindId(intval($_GET['id']));
        echo_json_exitc($params_data);
    }

    //删除接口申请
    public function actionDel(){
        $this->layout=false;
        $id=intval($_POST['id']);
        if(ApiUrlInfo::model()->IDeleteForId($id)){
            echo 't';
        }else{
            echo 'f';
        }
    }
    //删除接口申请
    public function actionDelParams(){
        $this->layout=false;
        $id=intval($_POST['id']);
        $apiId=intval($_POST['apiId']);
        if(ApiParamsInfo::model()->IDeleteForId($id,$apiId)){
            echo 't';
        }else{
            echo 'f';
        }
    }

    //接口调试demo
    public function actionTest(){
        echo_json_exitc($_REQUEST);
    }

    //接口请求历史参数历史列表
    public function actionAjaxParamsList(){
        $this->layout=false;
        $api_id=$_GET['id'];
        $per=5;//分页数
        if($api_id>0){
            $cnt=ApiParamsInfo::model()->CountForApiId($api_id);
            if($cnt>$per){
                $page = new PaginationAjax($cnt,$per,'params_page','','JsParamsPage');
                $page_list = $page->fpage(array(4,5,6));
            }
            $model=ApiParamsInfo::model()->ListForApiId($api_id,$page->limit);
        }
        $this->render('/api/ajax_params_list',array('data'=>$model,'page_list'=>$page_list));

    }

    ///demo
    public function actionInfo()
    {
        $tableName = $_GET['tableName'];
        $criteria = new CDbCriteria;
        $criteria->select = 'TABLE_NAME,ENGINE,AUTO_INCREMENT,TABLE_COMMENT';
        $criteria->condition = 'TABLE_SCHEMA=:a ';
        $criteria->params = array(':a' => $this->dataBase_name);
        $table_data = DevTables::model()->findAll($criteria);

        $criteria = new CDbCriteria;
        //$criteria->select='COLUMN_NAME,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT';
        $criteria->condition = 'TABLE_SCHEMA=:a and `TABLE_NAME`=:b ';
        $criteria->params = array(':a' => $this->dataBase_name ,':b' => $tableName);
        $columns_data = DevColumns::model()->findAll($criteria);

        $this->pageTitle=$tableName.' - '.$table_data[0]->TABLE_COMMENT;
        $this->render('/table/info' ,array('data' => $columns_data,'table_data'=>$table_data[0],));

    }

    public function actionAll()
    {
        $criteria = new CDbCriteria;

        $criteria->select = 'TABLE_NAME,ENGINE,AUTO_INCREMENT,TABLE_COMMENT';
        $criteria->condition = 'TABLE_SCHEMA=:a ';
        $criteria->params = array(':a' => $this->dataBase_name);
        $table_data = DevTables::model()->findAll($criteria);
        $info = array();
        foreach($table_data as $k => $v){
            $criteria = new CDbCriteria;
            $criteria->condition = 'TABLE_SCHEMA=:a and `TABLE_NAME`=:b ';
            $criteria->params = array(':a' => $this->dataBase_name ,':b' => $v['TABLE_NAME']);
            $info[$v['TABLE_NAME']] = DevColumns::model()->findAll($criteria);
        }

        $this->pageTitle='整库表字段';
        $this->render('all' ,array('table_data' => $table_data ,'info' => $info));

    }



}